#include <vector>
using namespace std;
class Solution1 {
public:
    int removeDuplicates(vector<int>& nums) {
        auto begin = nums.begin(), pcur = begin;
        while (begin != nums.end())
        {
            int k = 0;
            while (pcur < nums.end() && *begin == *pcur) ++pcur, ++k;
            if (k <= 2)
            {
                begin += k;
                pcur = begin;
            }
            else
            {
                begin = nums.erase(begin + 2, pcur);
                pcur = begin;
            }
        }
        return nums.size();
    }
};
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int slow = 2, fast = 2, n = nums.size();
        if (n <= 2)
        {
            return n;
        }
        while (fast < n)
        {
            if (nums[slow - 2] != nums[fast])
            {
                nums[slow++] = nums[fast++];
            }
            else fast++;
        }
        return slow;
    }
};